package name.jautzy.esi.client;

import name.jautzy.esi.client.model.EsiShip;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;


public class AddEftFittingListener extends SelectionListener<ButtonEvent> {
	private ListStore<EsiShip> sStore;
	
	public AddEftFittingListener (ListStore<EsiShip> sStore) {
		super();
		this.sStore = sStore;
	}
	
	@Override
	public void componentSelected(ButtonEvent ce) {  

        final Window formWindow = new Window();
        formWindow.setSize(600,450);
        formWindow.setPlain(false);
        formWindow.setModal(true);
        formWindow.setHeading("Import EFT Fit");
        formWindow.setLayout(new FormLayout());

        final TextArea area = new TextArea();
        area.setFieldLabel("EFT Fit");
        area.setName("eftfit");
        area.setAllowBlank(false);
        
        final CheckBox pvpBox = new CheckBox();
        pvpBox.setFieldLabel("Pvp ?");
        pvpBox.setValue(false);
        
        final CheckBox pveBox = new CheckBox();
        pveBox.setFieldLabel("Pve ?");
        pveBox.setValue(false);
        
        FormData data = new FormData (450,300);
        formWindow.add(area,data);
        formWindow.add(pvpBox);
        formWindow.add(pveBox);

        final Button btn = new Button("OK");  
        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
      
        	@Override  
        	public void componentSelected(ButtonEvent ce) {  
        	
        			AsyncCallback<EsiShip> callback = new AsyncCallback<EsiShip>() {
    	        	    public void onSuccess(EsiShip result) {
    	        	    	if (result != null) {
    	        	    		sStore.add(result);
    	        	    		sStore.commitChanges();
    	        	    		Esi.getEsi().addMessage("fit added : "+result.getShipName());
    	        	    	}
    	        	    	else
    	        	    		Esi.getEsi().addMessage("Can't add fit"); 
    	        	    	
    	        	    }

    	        	    public void onFailure(Throwable caught) {
    	        	    	Esi.getEsi().addMessage("Can't add fit : "+caught.toString()); 
    	        	    	
    	        	    }
    	        	  };
    	        	Esi.getEsi().addMessage("Parsing EFT and constructing Ship (may take some time)"); 
    	        	Esi.getEsi().getFitService().addShip(area.getValue(), pvpBox.getValue(), pveBox.getValue(), callback); 
    	        	formWindow.hide();
        		}  
   
        });  
        formWindow.addButton(btn);  
        formWindow.show();
	}
}
